package shuashuami.hb.com.avtivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.manager.HbAppManager;
import shuashuami.hb.com.share.ShareCUserInfo;

public class HbRegistered2Act extends AbActivity {

    private Button btnFinish;

    private String phone, password;

    private EditText edtRealName, edtQQ, edtEmail, edtdailibianma, identify, edtUserName;

    private LinearLayout llDailibianma, llShenfenzheng;
    private TextView tvDailibianma;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_hb_registered2);
        btnFinish = (Button) findViewById(R.id.btn_registered_2_finish);
        edtRealName = (EditText) findViewById(R.id.edt_registered_2_name);
        edtQQ = (EditText) findViewById(R.id.edt_registered_2_qq);
        edtEmail = (EditText) findViewById(R.id.edt_registered_2_email);
        edtdailibianma = (EditText) findViewById(R.id.edt_registered_2_dailibianma);
        identify = (EditText) findViewById(R.id.edt_registered_2_shenfenzheng);
        edtUserName = (EditText) findViewById(R.id.edt_registered_2_username);

        llDailibianma = (LinearLayout) findViewById(R.id.ll_registered_dailibianma);
        llShenfenzheng = (LinearLayout) findViewById(R.id.ll_registered_shenfenzheng);
        tvDailibianma = (TextView) findViewById(R.id.tv_dailibianma);

    }

    @Override
    protected void initData() {
        setTitleBar("注册(2/2)");
        setLeftView();
        /*获取Intent中的Bundle对象*/
        Bundle bundle = this.getIntent().getExtras();

	        /*获取Bundle中的数据，注意类型和key*/
        phone = bundle.getString("phone");
        password = bundle.getString("password");
        int type = HbAppManager.getInstance().getTypeValue();
        if (type == 2) {
            llDailibianma.setVisibility(View.GONE);
            //      llShenfenzheng.setVisibility(View.GONE);
        } else if (type == 3) {
            tvDailibianma.setText("推荐人ID");
        }
    }

    @Override
    protected void initListener() {
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 2:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) { //成功
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(HbRegistered2Act.this);
                            JSONObject temp = jsonObject.getJSONObject("content");
                            int tag = HbAppManager.getInstance().getTypeValue();
                            if (tag == 3) {
                                shareCUserInfo.setId(temp.getString("shopId"));
                            }
                            if (tag == 1) {
                                shareCUserInfo.setId(temp.getString("brushId"));
                            }
                            if (tag == 2) {
                                shareCUserInfo.setId(temp.getString("agentId"));
                            }
                            openActivity(HbMainAct.class);
                            finish();
                        } else {
                            openActivity(HbLoginAct.class);
                            ToastUtil.showShort(HbRegistered2Act.this, msg.obj.toString());
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(HbRegistered2Act.this, "数据解析错误");
                    }
                    break;
                case 3:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(HbRegistered2Act.this, jsonObject.getString("message"));
                            int tag = HbAppManager.getInstance().getTypeValue();
                            if (tag ==2) {
                                openActivity(HbLoginAct.class);
                            } else {
                                login();
                            }

                        } else {
                            ToastUtil.showShort(HbRegistered2Act.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void login() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        //显示ProgressDialog
        HttpMethods.login(listener,
                phone, password
        );
    }

    public void register() {

        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //   openActivityAndCloseThis(HbRegistered2Act.class);
                Message message = handler.obtainMessage(3);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };

        HttpMethods.register2(listener, phone, password,
                edtUserName.getText().toString(),
                edtRealName.getText().toString(),
                edtQQ.getText().toString(), edtEmail.getText().toString()
                , identify.getText().toString(), edtdailibianma.getText().toString()
        );
    }
}